package com.example.CarManagement.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.CarManagement.common.Result;
import com.example.CarManagement.sys.entity.LoginUser;
import com.example.CarManagement.sys.entity.Notification;
import com.example.CarManagement.sys.entity.Orders;
import com.example.CarManagement.sys.entity.User;
import com.example.CarManagement.sys.mapper.NotificationMapper;
import com.example.CarManagement.sys.service.INotificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements INotificationService {

    @Resource
    private UserServiceImpl userService;


    @Override
    public Result<?> selfNotice(String token) {
        LoginUser userInfo = userService.getUserInfo(token);


        User user = userInfo.getUser();

        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Notification::getUserId, user.getId());
        queryWrapper.orderByDesc(Notification::getCreateTime);
        List<Notification> notifications = this.baseMapper.selectList(queryWrapper);

        return Result.success(notifications);
    }



    @Override
    public Result<?> add(Notification notification, String token) {

        notification.setId(UUID.randomUUID().toString());

        notification.setCreateTime(LocalDateTime.now());

        notification.setUserId(userService.getUserInfo(token).getUserId());

        this.baseMapper.insert(notification);

        return Result.success("添加成功");
    }

    @Override
    public Result<?> selectPage(Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();


        Page<Notification> notificationPage = this.baseMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);


        Integer total = (int) notificationPage.getTotal();


        List<Notification> notifications = notificationPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("rows", notifications);
        data.put("total", total);

        return Result.success(data);
    }


}
